<template>
  <!--    <el-form v-model="dialogVisible" width="80%" title="操作步骤" destroy-on-close center>-->
    <el-form width="80%" title="关联产品" >
        <template #default>
            <!-- 工具栏部分开始 -->
            <el-row style="margin-top: 10px;">
                <el-col :span="24">
                    <el-tooltip
                            class="box-item"
                            effect="dark"
                            content="新增产品制成"
                            placement="top-start"
                    >
                        <el-button @click="toAddMat" type="primary"><el-icon><Plus /></el-icon></el-button>
                    </el-tooltip>
                    <el-button @click="editBtnHandler" :disabled="editBtnDisabled" type="primary"><el-icon><EditPen /></el-icon></el-button>
                    <el-popconfirm confirm-button-text="是" cancel-button-text="否"
                                   @confirm="deleteBtnHandler" title="您确定要删除选中的产品物料信息吗？">
                        <template #reference>
                            <el-button :disabled="deleteBtnDisabled" type="primary">
                                <el-icon>
                                    <Minus/>
                                </el-icon>
                            </el-button>
                        </template>
                    </el-popconfirm>
                </el-col>
            </el-row>
            <!-- 工具栏部分结束 -->
            <!-- 列表部分开始 -->
            <el-row style="margin-top: 10px;">
                <el-col :span="24">
                    <el-table ref="matTable" @selection-change="handleSelectionChange" :data="mats" border :stripe="true" style="width: 100%">
                        <el-table-column type="selection" width="55" />
                        <el-table-column prop="matCode" label="产品物料编码"  />
                        <el-table-column prop="matName" label="产品物料名称"  />
                        <el-table-column prop="matModel" label="规格型号"  />
                        <el-table-column prop="matUnitName" label="单位" />
                        <!-- 行工具栏 -->
                        <el-table-column width="140" label="操作">
                            <template #default="scope">
                                <el-button type="primary" @click="toEditMat(scope.row.matId)"  size="small">编辑</el-button>
                                <el-popconfirm confirm-button-text="是" cancel-button-text="否"
                                               @confirm="deleteMat(scope.row.matId)" title="您确定要删除这个关联产品？">
                                    <template #reference>
                                        <el-button type="danger"  size="small">删除</el-button>
                                    </template>
                                </el-popconfirm>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-col>
            </el-row>
            <!-- 列表部分结束 -->
            <!-- 分页部分开始 -->
            <el-row style="margin-top: 10px;">
                <el-col :span="24">
                    <el-pagination background @change="pageChange" layout="prev, pager, next" v-bind:page-size="limit" :total="total" />
                </el-col>
            </el-row>
            <!-- 分页部分结束 -->
        </template>
        <!-- footer插槽中放两个按钮 -->
        <template #footer>
            <el-row>
                <el-col :span="20"></el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="closeWin">关闭</el-button>
                </el-col>
            </el-row>
        </template>
    </el-form>
    <MatAddWin :visible="matAddWinDialogVisible"  :techniqueId="techniqueId" @optionCompleted="addMatOptionCompleted"/>
    <MatEditWin :visible="matEditWinDialogVisible" :techniqueId="techniqueId" :matId="matId" @optionCompleted="editMatOptionCompleted"/>
</template>

<script>
import {watch} from "vue";
import TechniqueMatRequest from '../../../../api/techniqueMat.js';

import MatAddWin from '../mat/mat-add-win.vue';
import MatEditWin from '../mat/mat-edit-win.vue';


export default {
    props:["techniqueId","matId"],// 属性
    emits:["optionCompleted"],// 暴露的事件
    name: "mat.vue",
    components:{
        MatEditWin,
        MatAddWin
    },
    data(){//数据模型
        return{
            dialogVisible: false, // 弹窗标记
            queryMat:{},// 绑定查询条件
            mats:[],//存储产品物料列表
            total:0,// 总记录数
            limit:10,// 每页显示的记录
            currentPage:1,// 当前页码
            editBtnDisabled:true,// 编辑按钮是否禁用
            deleteBtnDisabled:true,// 删除按钮是否禁用
            currentStepType:undefined,
            techniqueId:this.techniqueId,
            // techniqueId:'2',
            processId:'1004',
            editProcessVisible:false,
            editProcessStepId:-1,
            matId:'',
            matAddWinDialogVisible:false, // 添加物料产品弹窗显示标记
            matEditWinDialogVisible:false
        }
    },methods:{

        deleteMat(matId) {
            this.matId = matId;
            TechniqueMatRequest.deleteTechniqueMat(matId, res => {
                this.$message({
                    type: 'success',
                    message: '删除关联产品成功!'
                });
                this.loadMats(this.techniqueId);
            });
        },
        deleteBtnHandler(){
            let selectionRows = this.$refs.matTable.getSelectionRows();
            let matIds = new Array();
            selectionRows.forEach(row => {
                matIds.push(row.matId);
            })
            TechniqueMatRequest.deleteTechniqueMats(matIds,res =>{
                this.$message({
                    type: 'success',
                    message: '删除关联产品成功!'
                });
                this.loadMats(this.techniqueId);
            });
        },
        editBtnHandler(){
            let selectionRows = this.$refs.matTable.getSelectionRows()[0];
            if (selectionRows != undefined) {
                this.matId = selectionRows.matId;
                this.matEditWinDialogVisible = true;
            }else{
                this.$message({
                    type: 'warning',
                    message: '请选择一条记录进行编辑!'
                });
            }
        },
        editMatOptionCompleted(){
            this.matEditWinDialogVisible = false;
            this.loadMats(this.techniqueId);
        },
        /**
         * 添加关联产品成功回调
         */
        addMatOptionCompleted(){
            // 关闭弹窗
            this.matAddWinDialogVisible = false;
            // 重新加载数据
            this.loadMats(this.techniqueId);
        },
        /**
         * 翻页事件
         */
        pageChange(currentPage,currentLimit){

        },
        handleSelectionChange(){
            let selectionRows = this.$refs.matTable.getSelectionRows();
            if (selectionRows.length == 0) {
                this.editBtnDisabled = true;
                this.deleteBtnDisabled = true;
            } else if (selectionRows.length == 1) {
                this.editBtnDisabled = false;
                this.deleteBtnDisabled = false;
            } else {
                this.editBtnDisabled = true;
                this.deleteBtnDisabled = false;
            }
        },
        /**
         * 行编辑按钮事件
         * @param matId
         */
        toEditMat(matId){
            console.log("这里调用了编辑工序_______________"+matId)
            this.matId = matId;
            this.matEditWinDialogVisible = true;
        },
        /**
         * 添加关联产品弹窗
         */
        toAddMat(){
            this.matAddWinDialogVisible = true;
        },
        /**
         * 关闭当前的窗体
         */
        closeWin(){
            // 通知父窗体，操作完成
            this.$emit("optionCompleted");
        },
        /**
         * 加载关联产品列表
         */
        loadMats(techniqueId){
            // 准备参数
            this.queryMat.page = this.currentPage;
            this.queryMat.limit = this.limit;
            if(techniqueId)
                this.queryMat.techniqueId = techniqueId;
            TechniqueMatRequest.loadTechniqueMat(this.queryMat,res => {
                console.log(res)
                // 赋值
                this.mats = res.data.data;
                this.total = res.data.total;
            });
        }
    },mounted(props) {
        // this.loadMats();
        console.log(this.techniqueId)
        this.loadMats(this.techniqueId);
        // 侦听props   一旦发生变化，就会出发下面的函数
        watch(() => this.visible, (newVal, oldVal) => {
            // 将最新的值设置给弹窗的dialogVisible属性
            this.dialogVisible = newVal;
            if(this.dialogVisible){
                this.loadMats(this.techniqueId);
            }
        });
        watch(()=>this.techniqueId,(newVal,oldVal)=>{
            console.log(this.techniqueId)
            this.currentStepType = newVal;
            if(this.dialogVisible){
                this.loadMats(this.techniqueId);
            }
        });

    }
}
</script>

<style scoped>

</style>